package com.threeti.mecool.mobile.controller;

import com.threeti.mecool.core.application.project.ProjectService;
import com.threeti.mecool.core.domain.model.acl.User;
import com.threeti.mecool.core.domain.model.project.Project;
import com.threeti.mecool.core.domain.model.sellinto.SellInto;
import com.threeti.mecool.core.infrastructure.persistence.SellIntoRepository;
import com.threeti.mecool.core.infrastructure.persistence.acl.UserRepository;
import com.threeti.mecool.mobile.interfaces.BodyType;
import com.threeti.mecool.mobile.interfaces.ResponserDto;
import com.threeti.mecool.mobile.interfaces.assembler.SellIntoAssembler;
import com.threeti.mecool.mobile.interfaces.dto.SellIntoDto;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.threeti.mecool.mobile.Commander.command;
import static com.threeti.mecool.mobile.MessageConst.*;

/**
 * 卖进管理相关
 *
 * @author Jay Meng
 */
@Controller
@RequestMapping(value = "/sellinto")
public class SellIntoMobiController {
    private static final Logger logger = LoggerFactory.getLogger(SellIntoMobiController.class);

    @Autowired
    private UserRepository userRepo;

    @Autowired
    private ProjectService projService;

    @Autowired
    private SellIntoRepository sellIntoRepo;

    @RequestMapping(value = "/projectwithstoresandsellinto")
    @ResponseBody
    public ResponserDto before(@RequestParam(required = true) String userLoginName) {
        String command = command();
        User targetUser = userRepo.findByloginName(userLoginName);
        List<Project> projects = projService.findByUserOwn(targetUser);
        Collections.sort(projects, new Comparator<Project>() {
            @Override
            public int compare(Project o1, Project o2) {
                int val = o1.getCode().compareTo(o2.getCode());
                if (val == 0) {
                    val = o1.getName().compareTo(o2.getName());
                }
                return val;
            }
        });

        if (CollectionUtils.isEmpty(projects)) {
            return ResponserDto.withBizFailureMsg(command, MSG_COMMON_003);
        }

        return ResponserDto.successCommand(command).withBizSuccessBody(
                "projwithstoresandsellinto",
                SellIntoAssembler.toDtoList(projects, targetUser, projService, sellIntoRepo), BodyType.LIST);
    }

    @RequestMapping(value = "/editsellinto")
    @ResponseBody
    public ResponserDto editSellinto(@RequestBody SellIntoDto sellIntoDto) {
        String command = command();
        SellInto sellInto = SellIntoAssembler.toDom(sellIntoDto);

        if (sellIntoDto != null && StringUtils.isEmpty(sellIntoDto.getProjStoreId())) {
            return ResponserDto.withBizFailureMsg(command, MSG_SELLINTO_001);
        }

//    if (result.hasFieldErrors("projStoreId"))
//      return ResponserDto.withBizFailureMsg(command, MSG_SELLINTO_001);

        try {
            sellInto = sellIntoRepo.saveAndFlush(sellInto);
        } catch (Exception e) {
            logger.error(command, e);
            return ResponserDto.withBizFailureMsg(command, MSG_SELLINTO_002);
        }

        return ResponserDto.withBizSuccessMsg(command, MSG_COMMON_001);//.withMsgAttachement("sellinto", SellIntoAssembler.toDto(sellInto), BodyType.ENTITY);
    }
}
